package com.informix.stream.api.records;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CDCMetadataRecord {

    private int userData;
    private int flags;
    private int fixedLengthSize;
    private int fixedLengthCols;
    private int varLengthCols;
    private String colsCreateStmt;
    private Vector<String> colNames;
    private HashMap<String, String> colTypes;
    private HashMap<String, Integer> colLengths;
    private HashMap<String, Integer> colPrecisions;

    public CDCMetadataRecord(ByteBuffer buffer) {
        userData = buffer.getInt();

        // Must be 0
        flags = buffer.getInt();

        fixedLengthSize = buffer.getInt();

        fixedLengthCols = buffer.getInt();

        varLengthCols = buffer.getInt();
        byte[] coldesc = new byte[buffer.remaining()];
        buffer.get(coldesc);
        colsCreateStmt = new String(coldesc);
        parseColsCreateStmt();
    }

    private void parseColsCreateStmt() {
        colNames = new Vector<String>();
        colTypes = new LinkedHashMap<String, String>();
        colLengths = new LinkedHashMap<String, Integer>();
        colPrecisions = new LinkedHashMap<String, Integer>();
        Pattern pattern = Pattern.compile("(\\w+)\\s+(?:([\\w\\s]+)(?:\\(([^)]*)\\))?)[,$]?", Pattern.MULTILINE | Pattern.DOTALL);
        Matcher matcher = pattern.matcher(this.colsCreateStmt);
        while (matcher.find()) {
            String colName = matcher.group(1);
            colNames.add(colName);
            colTypes.put(colName, matcher.group(2));
            if (matcher.groupCount() > 2 && matcher.group(3) != null) {
                String lenPrec = matcher.group(3);
                if (lenPrec.contains(",")) {
                    colLengths.put(colName, Integer.parseInt(lenPrec.split(",")[0]));
                    colPrecisions.put(colName, Integer.parseInt(lenPrec.split(",")[1]));
                } else {
                    colLengths.put(colName, Integer.parseInt(lenPrec));
                    colPrecisions.put(colName, null);
                }
            } else {
                colLengths.put(colName, null);
                colPrecisions.put(colName, null);
            }
        }
    }

    public boolean hasUserData() {
        return true;
    }

    public int getUserData() {
        return userData;
    }

    public int getFlags() {
        return flags;
    }

    public int getFixedLengthSize() {
        return fixedLengthSize;
    }

    public int getFixedLengthCols() {
        return fixedLengthCols;
    }

    public int getVarLengthCols() {
        return varLengthCols;
    }

    public String getColsCreateStmt() {
        return colsCreateStmt;
    }

    public Vector<String> getColNames() {
        return colNames;
    }

    public HashMap<String, String> getColTypes() {
        return colTypes;
    }

    public HashMap<String, Integer> getColLengths() {
        return colLengths;
    }

    public HashMap<String, Integer> getColPrecisions() {
        return colPrecisions;
    }


}